package top.zackyoung.bbs.service.impl;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import top.zackyoung.bbs.dao.ArticleRepository;
import top.zackyoung.bbs.entity.ArticleList;
import top.zackyoung.bbs.entity.ResultMsg;
import top.zackyoung.bbs.entity.Search;
import top.zackyoung.bbs.service.ArticlesService;

import javax.annotation.Resource;

@Service
public class ArticlesServiceImp implements ArticlesService {

    @Resource
    private ArticleRepository articleRepository;


    @Override
    public ResultMsg getByArticleId(Integer articleId) {
        ArticleList byArticleId = articleRepository.findArticleListById(articleId);
        return new ResultMsg(200, "查询成功！", byArticleId);
    }

    @Override
    public ResultMsg getByArticleLike(Search search) {

        // 查询条件构建器
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();

        // 查询条件--多条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        // 条件1：必须
        boolQueryBuilder.must( QueryBuilders.matchQuery("title",search.getQ()));
        // 条件2：应该
        boolQueryBuilder.should( QueryBuilders.matchQuery("content", search.getQ()));

        queryBuilder.withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC));
        queryBuilder.withQuery(boolQueryBuilder);

        Page<ArticleList> articles = articleRepository.search(queryBuilder.build());
        return new ResultMsg(200, "查询成功！", articles);
    }

    @Override
    public ResultMsg findByUserId(Search search) {
        BoolQueryBuilder builder = QueryBuilders.boolQuery();
        builder.must(QueryBuilders.termQuery("userId", search.getUserId()));
        FieldSortBuilder sort = SortBuilders.fieldSort("articleVisitors").order(SortOrder.DESC);
        PageRequest page = PageRequest.of(search.getPage(), search.getSize());
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder()
                .withQuery(builder)
                .withPageable(page)
                .withSort(sort);
        NativeSearchQuery query = queryBuilder.build();

        Page<ArticleList> articles = articleRepository.search(query);
        return new ResultMsg(200, "查询成功！", articles);
    }

    @Override
    public ResultMsg findByTagId(Search search) {
// 查询条件构建器（存放所有的查询条件）
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();

        // 关键字查询 match
        queryBuilder.withQuery(QueryBuilders.matchQuery("tag_id",search.getType()));

        Page<ArticleList> articles = articleRepository.search(queryBuilder.build());
        return new ResultMsg(200, "查询成功！", articles);
    }

    @Override
    public ResultMsg deleteByArticleId(Integer articleId) {
        articleRepository.deleteById(articleId);
        ArticleList byArticleId = articleRepository.findArticleListById(articleId);
        if (byArticleId != null) {
            return new ResultMsg(500, "删除失败！", false);
        }
        return new ResultMsg(200, "删除成功！", true);
    }
}
